using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.Common.BL;
using NTTHAO.CommonWeb;

public partial class admin_webparts_URLSecurity_URLSecurityInput : System.Web.UI.UserControl
{
    public NTTHAO.Common.BL.AdminUser _LoggedUser = null;
    public DataTable _UserList = null;
    public string _ErrorMessage = "";
    public NTTHAO.Common.BL.URLMap _UrlMap = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);

        string strAction = Request.QueryString["action"];
        string strID = Request.QueryString["id"];

        try
        {
            _LoggedUser = new NTTHAO.Common.BL.AdminUser(Context.User.Identity.Name);
            if (_LoggedUser.ID < 1)
            {
                throw new Exception();
            }

            DataSet ds = _LoggedUser.GetAllByID(_LoggedUser.ID);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                _UserList = ds.Tables[0];
            }
        }
        catch (Exception)
        {
            System.Web.Security.FormsAuthentication.RedirectToLoginPage();
            return;
        }

        if (strAction != null)
        {
            if (strAction.Equals("edit", StringComparison.OrdinalIgnoreCase))
            {
                txtURL.Enabled = false;
                int id;
                try
                {
                    id = Int32.Parse(strID);
                    NTTHAO.Common.BL.URLMap urlMap = new NTTHAO.Common.BL.URLMap(id);

                    if (urlMap.ID > 0 && 
                        (urlMap.Editable == 1 || (urlMap.Editable == 0 && this._LoggedUser.UserName.Equals("admin", StringComparison.OrdinalIgnoreCase))))
                    {
                        this._UrlMap = urlMap;

                        urlID.Value = urlMap.ID.ToString();
                        txtURL.Text = urlMap.URL;
                        txtDescription.Text = urlMap.Description;
                        chkEditable.Checked = urlMap.Editable == 1 ? true : false;
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("?type=ser&action=list");
                }
            }
            else if (strAction.Equals("add", StringComparison.OrdinalIgnoreCase))
            {
                urlID.Value = "";
                txtURL.Text = "";
                txtDescription.Text = "";
                chkEditable.Checked = true;

                txtURL.Enabled = true;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string urlIDString = this.urlID.Value;
        int urlIDInt = 0;
        if (urlIDString.Trim().Length > 0)
        {
            try
            {
                urlIDInt = Int32.Parse(urlIDString);
            }
            catch (Exception)
            {
            }
        }

        try
        {

            NTTHAO.Common.BL.URLMap urlMap = new URLMap();

            if (urlIDInt < 1)
            {
                if (urlMap.ExistingURL(txtURL.Text.Trim()))
                {
                    this._ErrorMessage = LanguageUtility.GetLanguageText("EXISTING_URL", Session);
                    return;
                }
                //urlMap.ID = urlIDInt;
                urlMap.URL = txtURL.Text.Trim();
                urlMap.Description = txtDescription.Text.Trim();
                if (this._LoggedUser.UserName.Equals("admin", StringComparison.OrdinalIgnoreCase))
                {
                    urlMap.Editable = chkEditable.Checked ? 1 : 0;
                }
                else
                {
                    urlMap.Editable = 1;
                }
                urlMap.Insert();

                if (urlMap.ID > 0)
                {
                    // Insert new
                    string userAllowedListString = Request.Params["chkUser"];
                    if (userAllowedListString != null && userAllowedListString.Length > 0)
                    {
                        string[] userAllowedsString = userAllowedListString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < userAllowedsString.Length; i++)
                        {
                            URLSecurity urlSerItem = new URLSecurity();
                            urlSerItem.URLID = urlMap.ID;
                            urlSerItem.UserID = Int32.Parse(userAllowedsString[i].Trim());
                            urlSerItem.AllowRequest = 1;
                            urlSerItem.Insert();
                        }
                    }
                }
            }
            else
            {
                urlMap = new URLMap(urlIDInt);

                urlMap.Description = txtDescription.Text.Trim();
                if (this._LoggedUser.UserName.Equals("admin", StringComparison.OrdinalIgnoreCase))
                {
                    urlMap.Editable = chkEditable.Checked ? 1 : 0;
                }
                else
                {
                    urlMap.Editable = 1;
                }
                urlMap.Update();
                if (urlMap.ID > 0)
                {
                    // Delete all
                    URLSecurity urlSercurity = new URLSecurity();
                    urlSercurity.DeleteAllByUrlId(urlMap.ID);

                    // Insert new
                    string userAllowedListString = Request.Params["chkUser"];
                    if (userAllowedListString != null && userAllowedListString.Length > 0)
                    {
                        string[] userAllowedsString = userAllowedListString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < userAllowedsString.Length; i++)
                        {
                            URLSecurity urlSerItem = new URLSecurity();
                            urlSerItem.URLID = urlMap.ID;
                            urlSerItem.UserID = Int32.Parse(userAllowedsString[i].Trim());
                            urlSerItem.AllowRequest = 1;
                            urlSerItem.Insert();
                        }
                    }
                }
            }
        }
        catch (Exception) { }

        Response.Redirect("?type=ser&action=list");
    }
}
